package com.jscm.openmodule;

import com.jscm.openprocess.OpenProcessListView;
import com.jscm.openprocess.OpenProcessWindow;
import com.scm.all.export.ModuleOperationUtilsJNI;
import com.scm.all.export.ProcessAndThreadUtils;
import com.scm.all.export.SystemUtils;
import com.scm.all.export.WindowOperationUtils;
import com.scm.all.struct.TagModuleenTryX64;
import com.scm.all.struct.TagModuleenTryX86;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * 模块列表
 */
public class OpenModuleListView {
    public static String moduleHandle = "";
    public static ListView<String> listView = new ListView();
    public static ObservableList<String> observableList = FXCollections.observableArrayList();
    static {
        init();
    }

    public static void init() {
        observableList.clear();
        listView.getItems().clear();
        listView.setLayoutX(0);
        listView.setLayoutY(0);
        listView.setPrefWidth(784);
        listView.setPrefHeight(460);
        if(ModuleOperationUtilsJNI.systemIs32()){
            for(TagModuleenTryX86 tagModuleenTryX86 : ProcessAndThreadUtils.processPidToModulex86(OpenProcessListView.processHandler,null)){
                observableList.add(tagModuleenTryX86.hModule + "     " + tagModuleenTryX86.szExePath);
            }
        }
        if(ModuleOperationUtilsJNI.systemIs64()){
            for(TagModuleenTryX64 tagModuleenTryX64 : ProcessAndThreadUtils.processPidToModuleX64(OpenProcessListView.processHandler,null)){
                observableList.add(tagModuleenTryX64.hModule + "     " + tagModuleenTryX64.szExePath);
            }
        }
        listView.setItems(observableList);
        listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if(newValue!=null){
                    moduleHandle = newValue.split(" ")[0];
                }

            }
        });

        listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount()==2 && event.getButton().name().equals("PRIMARY")) {
                    setClipboardContents(moduleHandle);
                    OpenModuleWindow.stage.hide();
                }
            }
        });
    }
    private static void setClipboardContents(String v){
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(v);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }
}
